package com.example.aigc_education.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.aigc_education.domain.Paper;
import com.example.aigc_education.domain.PaperQuestion;
import com.example.aigc_education.domain.Question;
import com.example.aigc_education.domain.dto.GenPaperRuleDTO;
import com.example.aigc_education.domain.vo.QuestionVO;
import com.example.aigc_education.mapper.QuestionMapper;
import com.example.aigc_education.service.PaperQuestionService;
import com.example.aigc_education.service.PaperService;
import com.example.aigc_education.service.QuestionService;
import com.example.aigc_education.utils.ResultUtil;
import com.example.aigc_education.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/paper")
public class PaperController {

    @Resource
    private PaperService paperService;

    @Resource
    private PaperQuestionService paperQuestionService;

    @Resource
    private QuestionService questionService;

    /**
     * 自动组卷
     * @param paperName 试卷名
     * @param genPaperRuleDTO 组卷规则
     * @return 问题集合
     */
    @PostMapping("/autoGenerate")
    public ResultUtil autoGeneratePaper(String paperName,
                                        @RequestBody GenPaperRuleDTO genPaperRuleDTO) {
        Paper paper = new Paper();
        paper.setPaperName(paperName);
        paper.setStatus(1);
        boolean saved = paperService.save(paper);
        // saved成功后，paper.getPaperId()才有值
        if (!saved) {
            return ResultUtil.error("创建试卷失败");
        }

        List<QuestionVO> questionVOS = paperService.generatePaper(paper.getPaperId(),  genPaperRuleDTO);
        if (questionVOS == null) {
            return ResultUtil.error("自动组卷失败");
        }
        return ResultUtil.success(questionVOS);
    }

    /**
     * 更新试卷个别问题
     * 原则上，paperId和questionId是一一对应的，不可能出现一个试卷有多个相同的问题
     * @param paperId 试卷ID
     * @param questionId 问题ID
     * @return 返回更新试卷结果
     */
    @PutMapping("/update")
    public ResultUtil updatePaper(Long paperId, Long questionId) {
        QuestionVO questionVO = paperService.updatePaper(questionId, paperId);
        if (questionVO == null) {
            return ResultUtil.error("没有找到对应的试题");
        }
        return ResultUtil.success(questionVO);
    }

    /**
     * 获取试卷
     * @param paperId 试卷ID
     * @return 返回试卷
     */
    @GetMapping("/getPaperById")
    public ResultUtil getPaperByPageId(Long paperId) {
        LambdaQueryWrapper<PaperQuestion> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PaperQuestion::getPaperId, paperId);
        queryWrapper.eq(PaperQuestion::getStatus, 1);
        List<PaperQuestion> paperQuestions = paperQuestionService.list(queryWrapper);

        List<Long> questionIds = paperQuestions.stream()
                .map(PaperQuestion::getQuestionId)
                .distinct()
                .collect(Collectors.toList());

        log.info("questionIds: {}", questionIds);
        List<Question> questions = questionIds.stream()
            .map(questionId -> questionService.getOne(new LambdaQueryWrapper<Question>().eq(Question::getQuestionId, questionId)))
            .collect(Collectors.toList());
        return ResultUtil.success(questions);
    }

    /**
     * 获取用户创建的试卷
     * @return 返回用户的试卷
     */
    @GetMapping("/getPaperByUserId")
    public ResultUtil getPaperByUserId(){
        Long userId = SecurityUtils.getUserId();
        List<Paper> paperList = paperService.list(new LambdaQueryWrapper<Paper>().eq(Paper::getCreateBy, userId));
        return ResultUtil.success(paperList);
    }
}
